<template>
  <div class="main-app">
    <el-row style="margin-top: 50px">
      <el-form ref="form" :model="form" :rules="rules" label-width="600px">
        <el-col :span="15">
          <el-form-item label="设备名称:" prop="name">
            <el-input v-model="form.name" />
          </el-form-item>
          <el-form-item label="设备型号:" prop="model">
            <el-input v-model="form.model" />
          </el-form-item>
          <el-form-item label="位置:" prop="site">
            <el-input v-model="form.site" />
          </el-form-item>
          <el-form-item label="负责人姓名:" prop="personName">
            <el-select
              v-model="form.personName"
              placeholder="请选择负责人"
              clearable
              style="width: 100%"
              @change="personChange"
            >
              <el-option
                v-for="item in personNameList"
                :key="item.guid"
                :label="item.name"
                :value="item.guid"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="负责人电话:" prop="personPhone">
            <el-input v-model="form.personPhone" disabled />
          </el-form-item>
          <el-form-item label="状态:" prop="status">
            <el-radio-group v-model="form.status">
              <el-radio
                v-for="(item, index) in options"
                :key="index"
                :label="item.value"
              >{{ item.label }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
    <el-row style="margin-top: 40px; text-align: center">
      <el-form :inline="true" class="form-inline">
        <el-form-item>
          <el-button type="primary" @click="save">保存</el-button>
        </el-form-item>
        <el-form-item>
          <el-button @click="cancel">取消</el-button>
        </el-form-item>
      </el-form>
    </el-row>
  </div>
</template>

<script>
import { EquipmentStatus } from "../../utils/resource";
import { save, getEquipmentById } from "../../api/equipment";
import { getUserByEnterpriseId } from "../../api/user";
export default {
  data() {
    return {
      form: {
        name: "",
        model: "",
        site: "",
        personName: "",
        personPhone: "",
        status: "",
      },
      personNameList: [],
      options: EquipmentStatus,
      rules: {
        name: [{ required: true, message: "请输入设备名称", trigger: "blur" }],
        model: [{ required: true, message: "请输入设备型号", trigger: "blur" }],
        site: [{ required: true, message: "请输入位置", trigger: "blur" }],
        personName: [
          { required: true, message: "请输负责人姓名", trigger: "blur" },
        ],
        personPhone: [
          { required: true, message: "请输负责人电话", trigger: "blur" },
        ],
        status: [{ required: true, message: "请选择状态", trigger: "change" }],
      },
    };
  },
  async created() {
    const { id } = this.$route.params;
    if (id) {
      this.id = id;
      await this.getEquipmentById(id);
    }

    const { data } = await getUserByEnterpriseId();
    this.personNameList = data;
  },
  methods: {
    personChange() {
      this.form.personPhone = this.personNameList.find(
        (p) => p.guid === this.form.personName
      ).phone;
    },
    async save() {
      this.$refs["form"].validate(async(valid) => {
        if (valid) {
          let data = {
            name: this.form.name,
            model: this.form.model,
            site: this.form.site,
            personId: this.form.personName,
            personName: this.personNameList.find(
              (p) => p.guid === this.form.personName
            ).name,
            personPhone: this.form.personPhone,
            status: parseInt(this.form.status),
          };
          console.log("data:", data);
          console.log(2323);
          let message = "";
          if (!this.id) {
            message = "设备添加成功";
          } else {
            data = Object.assign({ id: this.id }, data);
            message = "设备修改成功";
          }
          await save(data);
          this.$notify({
            title: "成功",
            message: message,
            type: "success",
            duration: 3000,
          });
          this.$router.push("/equipment");
        } else {
          return false;
        }
      });
    },
    cancel() {
      this.$router.push("/equipment");
    },
    async getEquipmentById(id) {
      const { data } = await getEquipmentById(id);
      console.log(data)
      this.form.name = data.name;
      this.form.model = data.model;
      this.form.site = data.site;
      this.form.personName = data.personId;
      this.form.personPhone = data.personPhone;
      this.form.status = data.status;
    },
  },
};
</script>
